package com.exchange.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import com.jfinal.core.Controller;
import com.jfinal.json.Json;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;


/**
 * 基础控制器
 *
 * @author 张烨
 **/
public abstract class BaseController extends Controller {
	
    
	
	 /**
     * 获取分页码
     *
     * @return
     */
    protected int getPage() {
        return getParaToInt("page", 1);
    }

    /**
     * 获取分页条数
     *
     * @return
     */
    protected int getRows() {
        return getParaToInt("rows", 15);
    }

    /**
     * 排序方式
     *
     * @return
     */
    protected String getSord() {
        String sord = getPara("sord", "desc");
        return sord;
    }

    /**
     * 排序字段
     *
     * @return
     */
    protected String getSidx() {
        String sidx = getPara("sidx", "id");
        return sidx;
    }

    /**
     * 封装jqgrid分页数据
     *
     * @param pageInfo
     * @return
     */
    protected Map<String, Object> getPage(Page pageInfo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("rows", pageInfo.getList());
        map.put("total", pageInfo.getTotalPage());
        map.put("records", pageInfo.getTotalRow());
        map.put("page", pageInfo.getPageNumber());
        return map;
    }

    /**
     * 获取最小查询时间
     *
     * @return
     */
    protected String getMinTime() {
        String minTime = getPara("min", "");
        return minTime;
    }

    /**
     * 将post的json结构体转成Bean
     *
     * @param clazz 类
     * @return Bean
     */
    public <T> T getJsonReqBody(Class<T> clazz) {
        String jsonString = getReqBody();
        if (StrKit.isBlank(jsonString)) {
            return null;
        }
        return Json.getJson().parse(jsonString, clazz);
    }

    /**
     * 获取post的结构体
     *
     * @return json or xml字符串
     */
    public String getReqBody() {
        return HttpKit.readData(getRequest());
    }

    /**
     * 成功并携带数据
     *
     * @param data
     */
    public void renderSuccess(Object data) {
        Record record = new Record();
        record.set("result", true);
        record.set("data", data);
        renderJson(record);
    }
    /**
     * 成功并自定义提示消息
     *
     * @param string
     */
    public void renderSuccess(String string) {
        Record record = new Record();
        record.set("result", true);
        record.set("msg", string);
        renderJson(record);
    }

    /**
     * 成功
     *
     * @param
     */
    public void renderSuccess() {
        Record record = new Record();
        record.set("result", true);
        renderJson(record);
    }

    /**
     * 失败信息
     *
     * @param string
     */
    public void renderError(String string) {
        Record record = new Record();
        record.set("result", false);
        record.set("msg", string);
        renderJson(record);
    }
    
}
